﻿Library.UserInterface.Widgets.Weather = function (options) {
    var defaults = {
        container: null,
        country: "Ankara",        
        titleSelector: "",
        onSettingsSave: "",
        interval: 120000        
    };
    var options = $.extend(defaults, options);

    this.container = options.container;
    this.country = options.country;
    this.titleSelector = options.titleSelector;
    this.onSettingsSave = options.onSettingsSave;
    this.interval = options.interval;

    this.initialize(false);
}

Library.UserInterface.Widgets.Weather.prototype = {
    container: null,
    country: null,
    titleSelector: "",
    onSettingsSave: "",
    interval: null,
    fullscreen: true,
    minimize: true,
    settings: true,
    id: null,
    type: "widget.weather",

    initialize: function (set) {
        if (this.id != null)
            Library.UserInterface.Widgets.Dispose(this.id);

        this.id = Library.Util.ReplaceAll(Library.Crypto.Guid(), "-", "");
        this.container.find(".portlet-widget").html(Library.UserInterface.Widgets.Weather.Lang.loading);

        Library.UserInterface.Widgets.Add(this);

        if (this.titleSelector == "")
            this.titleSelector = this.container.find(".portlet-title").selector;

        if (this.titleSelector != "")
            $(this.titleSelector).html(Library.UserInterface.Widgets.Weather.Lang.loading);
        try {
            if (set) {
                this.container.find(".portlet-widget").toggle("blind", {}, 500);
                this.container.find(".portlet-widget").parents(".portlet-content").find(".portlet-settings").toggle("blind", {}, 500);
            }
            var widget = this;
            $(document).everyTime(this.interval, this.id,
                function (i) {
                    $.get("/Util/XmlDoor.aspx", { Url: "http://www.google.com/ig/api?weather=" + widget.country + "&hl=" + Library.UserInterface.Widgets.Weather.Lang.lang + "&id=" + widget.id }, widget.getWeather);
                }
            );
            this.getSettings();
            Library.UserInterface.Widgets.SetHeaderIcons(this);
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err);
        }
    },

    getWeather: function (data) {
        var widget = Library.UserInterface.Widgets.Get(unescape(this.url).substr(unescape(this.url).indexOf("id=") + 3));
        var html = "";
        html += "<table cellpadding='0' cellspacing='0' border='0' class='Weather'>";
        html += "	<tr>";
        html += "		<td style='vertical-align:top; text-align:left; width:140px'><img width='120px' src='/Images/Weather{0}' alt='' /></span></td>";
        html += "		<td style='vertical-align:top'>";
        html += "			<table cellpadding='0' cellspacing='0' border='0' style='text-align:left;'>";
        html += "				<tr>";
        html += "					<td><span class='Heat'>{1}</span></td>";
        html += "				</tr>";
        html += "				<tr>";
        html += "					<td><span class='Status'>{2}</span></td>";
        html += "				</tr>";
        html += "				<tr>";
        html += "					<td><span class='Humidity'>{3}</span></td>";
        html += "				</tr>";
        html += "				<tr>";
        html += "					<td><span class='Wind'>{4}</span></td>";
        html += "				</tr>";
        html += "			</table>";
        html += "		</td>";
        html += "	</tr>";
        html += "	<tr>";
        html += "		<td colspan='2'>";
        html += "			<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
        html += "				<tr>";
        html += "					<td><span class='DayName'>{5}</span></td>";
        html += "					<td><span class='DayName'>{6}</span></td>";
        html += "					<td><span class='DayName'>{7}</span></td>";
        html += "					<td><span class='DayName'>{8}</span></td>";
        html += "				</tr>";
        html += "				<tr>";
        html += "					<td><img width='90' src='/Images/Weather{9}' title='{13}'/></td>";
        html += "					<td><img width='90' src='/Images/Weather{10}' title='{14}'/></td>";
        html += "					<td><img width='90' src='/Images/Weather{11}' title='{15}'/></td>";
        html += "					<td><img width='90' src='/Images/Weather{12}' title='{16}'/></td>";
        html += "				</tr>";
        html += "				<tr>";
        html += "					<td><span class='DayDeg' >{17}</span> <span class='NightDeg' >{21}</span></td>";
        html += "					<td><span class='DayDeg' >{18}</span> <span class='NightDeg' >{22}</span></td>";
        html += "					<td><span class='DayDeg' >{19}</span> <span class='NightDeg' >{23}</span></td>";
        html += "					<td><span class='DayDeg' >{20}</span> <span class='NightDeg' >{24}</span></td>";
        html += "				</tr>";
        html += "			</table>";
        html += "		</td>";
        html += "	</tr>";
        html += "</table>";

        try {
            html = html.replace("{0}", $(data).find("current_conditions").find("icon").attr("data").replace(".gif", ".png"));
            html = html.replace("{1}", $(data).find("current_conditions").find("temp_c").attr("data") + "°C");
            html = html.replace("{2}", $(data).find("current_conditions").find("condition").attr("data"));
            html = html.replace("{3}", $(data).find("current_conditions").find("humidity").attr("data"));
            html = html.replace("{4}", $(data).find("current_conditions").find("wind_condition").attr("data"));
            html = html.replace("{5}", Library.UserInterface.Widgets.Weather.Lang.today);
            html = html.replace("{6}", Library.UserInterface.Widgets.Weather.Lang.tomorrow);
            html = html.replace("{7}", eval("Library.UserInterface.Widgets.Weather.Lang.days." + $($(data).find("forecast_conditions")[2]).find("day_of_week").attr("data")));
            html = html.replace("{8}", eval("Library.UserInterface.Widgets.Weather.Lang.days." + $($(data).find("forecast_conditions")[3]).find("day_of_week").attr("data")));
            html = html.replace("{9}", $($(data).find("forecast_conditions")[0]).find("icon").attr("data").replace(".gif", ".png"));
            html = html.replace("{10}", $($(data).find("forecast_conditions")[1]).find("icon").attr("data").replace(".gif", ".png"));
            html = html.replace("{11}", $($(data).find("forecast_conditions")[2]).find("icon").attr("data").replace(".gif", ".png"));
            html = html.replace("{12}", $($(data).find("forecast_conditions")[3]).find("icon").attr("data").replace(".gif", ".png"));
            html = html.replace("{13}", $($(data).find("forecast_conditions")[0]).find("condition").attr("data"));
            html = html.replace("{14}", $($(data).find("forecast_conditions")[1]).find("condition").attr("data"));
            html = html.replace("{15}", $($(data).find("forecast_conditions")[2]).find("condition").attr("data"));
            html = html.replace("{16}", $($(data).find("forecast_conditions")[3]).find("condition").attr("data"));
            html = html.replace("{17}", $($(data).find("forecast_conditions")[0]).find("high").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{18}", $($(data).find("forecast_conditions")[1]).find("high").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{19}", $($(data).find("forecast_conditions")[2]).find("high").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{20}", $($(data).find("forecast_conditions")[3]).find("high").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{21}", $($(data).find("forecast_conditions")[0]).find("low").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{22}", $($(data).find("forecast_conditions")[1]).find("low").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{23}", $($(data).find("forecast_conditions")[2]).find("low").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);
            html = html.replace("{24}", $($(data).find("forecast_conditions")[3]).find("low").attr("data") + "°" + Library.UserInterface.Widgets.Weather.Lang.unit);

            widget.container.find(".portlet-widget").html(html);
            if (widget.titleSelector != "")
                $(widget.titleSelector).html(Library.Util.ReplaceAll(Library.UserInterface.Widgets.Weather.Lang.title, "{0}", $(data).find("postal_code").attr("data")));
        } catch (err) {
            Library.UserInterface.Widgets.Error(widget.id, err);
        }
    },

    getSettings: function () {
        var settingsHTML = "";
        settingsHTML += "<table class=\"Forms\"><tr><td class=\"Standart\">" + Library.UserInterface.Widgets.Weather.Lang.select_location + "</td><td>";
        settingsHTML += "<input type=\"text\" class=\"Text ui-weather-location\" />";
        settingsHTML += "</td><td class=\"Buttons\">";
        settingsHTML += "<input type=\"button\" value=\"" + Library.UserInterface.Widgets.Weather.Lang.save + "\" style=\"font-size:12px; width:60px\" onclick=\"Library.UserInterface.Widgets.Get('" + this.id + "').save($(this).parents('.Forms').find('.ui-weather-location').val())\"/>";
        settingsHTML += "</td></tr></table>";
        this.container.find(".portlet-settings").html(settingsHTML);
    },

    save: function (country) {
        $(document).stopTime(this.id);
        this.country = country;
        if (jQuery.isFunction(this.onSettingsSave))
            eval(this.onSettingsSave + "('" + country + "')");
        this.initialize(true);
    }
};